<?php

namespace Modules\Admin\Service;

use HiCommon\Repository\MaintainConfigRepository;
use HiCommon\Repository\MaintainShopRepository;

class MaintainConfigService
{

    /**
     * 获取所有权限
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all()
    {
        try {
            $arr_option = ['order' => ['key' => 'id', 'value' => 'asc'], 'field' => ['id', 'pid', 'name'], 'where' => ['pid' => 0], 'with' => ['son']];

            $data = MaintainConfigRepository::get_all($arr_option)->toarray();

            return hello_success('成功', $data);
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 获取分页数据
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list()
    {
        try {
            $arr_option = ['order' => ['key' => 'id', 'value' => 'asc'], 'field' => ['id', 'pid', 'name', 'created_at'], 'where' => ['pid' => 0], 'with' => ['son']];
            $data = MaintainConfigRepository::get_list($arr_option);
            $data = list_page_data($data);
            return hello_success('成功', $data);
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 添加一条数据
     *
     * @param $arr_input
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_one($arr_input)
    {
        $arr_input = hi_array_del_null($arr_input);
        try {
            $father = ['pid' => 0, 'name' => $arr_input['name']];
            $res = MaintainConfigRepository::add_one($father)->toarray();

            $son = [];
            foreach ($arr_input['son'] as $kay => $value) {
                $son[] = [
                    'pid' => $res['id'],
                    'name' => $value['name'],
                    'explain' => $value['explain']
                ];
            }

            $result = MaintainConfigRepository::add_many($son);
            if (empty($res) || empty($result)) {
                return hello_error('添加失败');
            }

            return hello_success('成功', $res);
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one(int $n_id)
    {
        try {
            $n_id = (int)$n_id;
            if ($n_id <= 0) {
                hello_error('数据ID不能为空');
            }
            //查询
            $arr_option = ['field' => ['id', 'pid', 'name', 'created_at'], 'with' => ['son']];
            $res = MaintainConfigRepository::get_one($n_id, $arr_option)->toarray();
            if (empty($res)) {
                return hello_error('查询失败');
            }

            return hello_success('查询成功', $res);
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 修改一条数据
     *
     * @param array $arr_input
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_one(array $arr_input)
    {
        $arr_input = hi_array_del_null($arr_input);
        try {
            $arr_input['id'] = (int)$arr_input['id'];
            if ($arr_input['id'] <= 0) {
                return hello_error('数据id不能为空');
            }
            $son = $arr_input['son'];
            unset($arr_input['son']);

            $res = MaintainConfigRepository::edit_one($arr_input);
            foreach ($son as $kay => $value) {
                $arr_where['id'] = $value['id'];
                $son = [
                    'id' => $value['id'],
                    'pid' => $arr_input['id'],
                    'name' => $value['name'],
                    'explain' => $value['explain']
                ];
                MaintainConfigRepository::edit_or_create($arr_where, $son);
            }

            if ($res === false) {
                return hello_error('操作失败');
            }
            return hello_success('成功');
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 修改单个字段
     *
     * @param $arr_input
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_one_state($arr_input)
    {
        try {
            $arr_input['id'] = (int)$arr_input['id'];
            if ($arr_input['id'] <= 0) {
                return hello_error('数据id不能为空');
            }

            $res = MaintainConfigRepository::edit_one($arr_input);
            if ($res === false) {
                return hello_error('操作失败');
            }
            return hello_success('成功');
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 删除一条数据
     *
     * @param int $n_id
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one($n_id)
    {
        try {
            if ($n_id <= 0) {
                return hello_error('数据id不能为空');
            }
            //检测是否可以删除
            $num = MaintainShopRepository::check_config((int)$n_id);
            if ($num > 0) {
                return hello_error('该配置已被使用不可删除！');
            }

            $res = MaintainConfigRepository::delete_one($n_id);
            if ($res === false) {
                return hello_error('操作失败');
            }
            return hello_success('成功');

        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

}